.TH std::codecvt_byname 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::codecvt_byname \- std::codecvt_byname

.SH Synopsis
   Defined in header <locale>
   template< class InternT, class ExternT, class State >
   class codecvt_byname : public std::codecvt<InternT, ExternT, State>;

   std::codecvt_byname is a std::codecvt facet which encapsulates multibyte/wide
   character conversion rules of a locale specified at its construction.

.SH Specializations

   The standard library is guaranteed to provide the following specializations:

   Defined in header <locale>
   std::codecvt_byname<char, char,        identity conversion
   std::mbstate_t>
   std::codecvt_byname<char16_t, char,    conversion between UTF-16 and UTF-8 (since
   std::mbstate_t>                        C++11)(deprecated in C++20)
   std::codecvt_byname<char16_t, char8_t, conversion between UTF-16 and UTF-8 (since
   std::mbstate_t>                        C++20)
   std::codecvt_byname<char32_t, char,    conversion between UTF-32 and UTF-8 (since
   std::mbstate_t>                        C++11)(deprecated in C++20)
   std::codecvt_byname<char32_t, char8_t, conversion between UTF-32 and UTF-8 (since
   std::mbstate_t>                        C++20)
   std::codecvt_byname<wchar_t, char,     locale-specific conversion between wide
   std::mbstate_t>                        string and narrow character sets

.SH Member functions

   constructor   constructs a new codecvt_byname facet
                 \fI(public member function)\fP
   destructor    destroys a codecvt_byname facet
                 \fI(protected member function)\fP



std::codecvt_byname::codecvt_byname

   explicit codecvt_byname( const char* name, std::size_t refs = 0 );
   explicit codecvt_byname( const std::string& name, std::size_t refs =   \fI(since C++11)\fP
   0 );

   Constructs a new std::codecvt_byname facet for a locale with name.

   refs is used for resource management: if refs == 0, the implementation destroys the
   facet, when the last std::locale object holding it is destroyed. Otherwise, the
   object is not destroyed.

.SH Parameters

   name - the name of the locale
   refs - the number of references that link to the facet



std::codecvt_byname::~codecvt_byname

   protected:
   ~codecvt_byname();

   Destroys the facet.

Inherited from std::codecvt

.SH Member types

   Member name Definition
   intern_type internT
   extern_type externT
   state_type  stateT

.SH Member objects

   Member name Type
   id (static) std::locale::id

.SH Member functions

                 invokes do_out
   out           \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_in
   in            \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_unshift
   unshift       \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_encoding
   encoding      \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_always_noconv
   always_noconv \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_length
   length        \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP

                 invokes do_max_length
   max_length    \fI(public member function of std::codecvt<InternT,ExternT,StateT>)\fP


.SH Protected member functions

                    converts a string from InternT to ExternT, such as when writing to
   do_out           file
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    converts a string from ExternT to InternT, such as when reading
   do_in            from file
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    generates the termination character sequence of ExternT characters
   do_unshift       for incomplete conversion
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    returns the number of ExternT characters necessary to produce one
   do_encoding      InternT character, if constant
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    tests if the facet encodes an identity conversion for all valid
   do_always_noconv argument values
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    calculates the length of the ExternT string that would be consumed
   do_length        by conversion into given InternT buffer
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)
                    returns the maximum number of ExternT characters that could be
   do_max_length    converted into a single InternT character
   \fB[virtual]\fP        \fI\fI(virtual protected member function\fP of\fP
                    std::codecvt<InternT,ExternT,StateT>)

Inherited from std::codecvt_base

   Member type                                 Definition
   enum result { ok, partial, error, noconv }; Unscoped enumeration type

   Enumeration constant Definition
   ok                   conversion was completed with no error
   partial              not all source characters were converted
   error                encountered an invalid character
   noconv               no conversion required, input and output types are the same

.SH Example

   This example demonstrates reading a GB18030-encoded file using the codecvt facet
   from a GB18030-aware locale.


// Run this code

 #include <fstream>
 #include <iostream>
 #include <locale>
 #include <string>

 int main()
 {
     // GB18030 narrow multibyte encoding
     std::ofstream("text.txt") << "\\x7a"              // letter 'z', U+007a
                                  "\\x81\\x30\\x89\\x38"  // letter 'ß', U+00df
                                  "\\xcb\\xae"          // CJK ideogram '水' (water), U+6c34
                                  "\\x94\\x32\\xbc\\x35"; // musical sign '𝄋' (segno), U+1d10b

     std::wifstream fin("text.txt");
     fin.imbue(std::locale(fin.getloc(),
               new std::codecvt_byname<wchar_t, char, std::mbstate_t>("zh_CN.gb18030")));

     for (wchar_t c; fin.get(c);)
         std::cout << std::hex << std::showbase << static_cast<unsigned>(c) << '\\n';
 }

.SH Possible output:

 0x7a
 0xdf
 0x6c34
 0x1d10b

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR   Applied to           Behavior as published               Correct behavior
                     the standard library did not need to       two specializations are
   LWG 21 C++98      provide                                    required
                     any std::codecvt_byname specializations

.SH See also

   codecvt converts between character encodings, including UTF-8, UTF-16, UTF-32
           \fI(class template)\fP
